package 算法.leetcode.offer;

import 算法.leetcode.offer.base.ListNode;

/**
 * 输入一个链表的头节点，从尾到头反过来返回每个节点的值（用数组返回）。
 *
 *
 *
 * 示例 1：
 *
 * 输入：head = [1,3,2]
 * 输出：[2,3,1]
 * @author lchenglong
 * @date 2022/3/21
 */
public class Offer06 {
    public int[] reversePrint(ListNode head) {

        int len = 0;
        ListNode tmp,pre= null;
        while(head!=null){
            len++;
            tmp = head.next;
            head.next = pre;
            pre = head;
            if (null == tmp)break;
            head = tmp;
        }
        int[] ans =new int[len];
        len = 0;
        while(head!=null){
            ans[len++] = head.val;
            head = head.next;
        }
        return ans;
    }
}
